﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BLL;
using ChamCongMain.Form;
using System.Diagnostics;

namespace ChamCongMain.UserControl
{
    public partial class uclDonVi : DevExpress.XtraEditors.XtraUserControl
    {
        DonViBLL donViBLL = new DonViBLL();
        public uclDonVi()
        {
            InitializeComponent();
            gridDonVi.DataSource = donViBLL.GetAllDonVi();
        }

        private void RefeshG(object formName)
        {
            gridDonVi.DataSource = formName;
        }

        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            frmDonVi frm = new frmDonVi(ChucNang.Them, null);
            frm.LoadData = new Delegates.RefeshGrid(RefeshG);
            frm.ShowDialog();
            this.Cursor = Cursors.Default;
        }

        private void btnSua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            DonVi cn = (DonVi)gvDonVi.GetRow(gvDonVi.FocusedRowHandle);
            if (cn == null)
            {
                XtraMessageBox.Show("Không có dữ liệu để sửa !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                frmDonVi frm = new frmDonVi(ChucNang.Sua, cn);
                frm.LoadData = new Delegates.RefeshGrid(RefeshG);
                frm.ShowDialog();
            }
            this.Cursor = Cursors.Default;
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            DonVi cn = (DonVi)gvDonVi.GetRow(gvDonVi.FocusedRowHandle);
            if (cn == null)
            {
                this.Cursor = Cursors.Default;
                return;
            }
            else
            {
                DonViBLL cnbll = new DonViBLL();
                try
                {
                    cnbll.DeleteDonVi(cn);
                    XtraMessageBox.Show("Xoá thành công!", "Xóa");
                    gvDonVi.DeleteSelectedRows();
                }
                catch (Exception ee)
                {
                    XtraMessageBox.Show(ee.ToString(), "Error");
                }
            }
            this.Cursor = Cursors.Default;
        }

        private void btnXuatExcel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (gvDonVi.RowCount == 0)
            {
                XtraMessageBox.Show("Không có dữ liệu để xuất file !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Cursor = System.Windows.Forms.Cursors.Default;
                return;
            }
            DevExpress.XtraGrid.Views.Grid.GridView grid = gridDonVi.MainView as DevExpress.XtraGrid.Views.Grid.GridView;
            if (grid != null)
            {
                SaveFileDialog saveExcel = new SaveFileDialog();
                saveExcel.Filter = "Excel Workbook|*.xlsx|Excel Macro-Enabled Workbook|*.xlsm|Excel 97-2003 Workbook|*.xls";// định giạng file lưu
                saveExcel.Title = "Save an Excel File";
                saveExcel.InitialDirectory = @"C:\";
                if (saveExcel.ShowDialog() == DialogResult.OK)
                {
                    this.Cursor = System.Windows.Forms.Cursors.Default;
                    grid.ExportToXlsx(saveExcel.FileName);
                    XtraMessageBox.Show("Export File thành công!!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (XtraMessageBox.Show("Bạn có muốn xem file ?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        Process exportXlsx = new Process();
                        exportXlsx.StartInfo.FileName = "EXCEL.exe";
                        exportXlsx.StartInfo.Arguments = saveExcel.FileName;
                        exportXlsx.Start();
                    }
                }
            }
            this.Cursor = Cursors.Default;
        }

        private void btnTimKiem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string kytu = string.Empty;
            kytu = txtSearch.EditValue.ToString();
            gridDonVi.DataSource = donViBLL.SearchByTen(kytu);
        }
    }
}
